﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Entity.Bet;
using LotteryVote.Logic;
using LotteryVote.Utility;
using LotteryVote.Entity;
using LotteryVote.Logic.Web;
using System.Text;
using System.IO;

namespace LotteryVote
{
    public partial class BetVote : MemberPage
    {
        public IList<LotteryCompany> TodayCompany { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            TodayCompany = TodayLotteryCompany.Instance.GetTodayCompany();
            WriteCompanyNumLenToClient();
        }

        protected void btn_getJson_Click(object sender, EventArgs e)
        {
            var companys = TodayLotteryCompany.Instance.GetTodayCompany();
            IList<BetItem> betList = new List<BetItem>();
            for (int i = 0; i < 10; i++)
            {
                BetItem bet = new BetItem
                {
                    Num = i + "10000",
                    CompanyList = companys.Select(it => it.CompanyId).ToArray(),
                    WargerList = new List<LotteryVote.Entity.Bet.BetItem.WagerItem>()
                };
                for (int j = 0; j < 10; j++)
                {
                    LotteryVote.Entity.Bet.BetItem.WagerItem wager = new BetItem.WagerItem
                    {
                        GamePlayTypeId = j,
                        IsFullPermutation = j % 2 == 0,
                        Wager = i * j * 99
                    };
                    bet.WargerList.Add(wager);
                }
                betList.Add(bet);
            }
            Response.Write(JSONHelper.ToJson(betList));
        }

        protected void btn_AddBet_Click(object sender, EventArgs e)
        {
            var betList = JSONHelper.ToObject<List<BetItem>>(hd_betInfo.Value);
            UserFacade userFacade = new UserFacade();
            BetFacade betFacade = new BetFacade();
            string message = betFacade.AddBet(CurrentUser, 1, betList);
            lbl_message.Text = message;
        }

        private void WriteCompanyNumLenToClient()
        {
            NumLenFacade numLenFacade = new NumLenFacade();
            List<CompanyNumLen> numLens = new List<CompanyNumLen>();
            foreach (var company in TodayCompany)
            {
                var numLenSupport = numLenFacade.GetNumLengthByCompanyId(company.CompanyId);
                var lenCount = numLenSupport.Select(it => new LotteryVote.Entity.CompanyNumLen.NumLenItem { Length = it.Length.Length, Count = it.Count });
                numLens.Add(new CompanyNumLen
                 {
                     CompanyId = company.CompanyId,
                     NumLenList = lenCount.ToList()
                 });
            }
            var baseStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(JSONHelper.ToJson(numLens)));
            CookieSerializer cs = new CookieSerializer("CompanyNumLen", baseStr);
            cs.SaveCookie();
        }
    }
}